/*
 * Copyright 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.samples.apps.nowinandroid.demo.viewmodel

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.gms.common.util.CollectionUtils.listOf
import com.google.samples.apps.nowinandroid.demo.api.MovieApi
import com.google.samples.apps.nowinandroid.demo.model.MovieInfo
import com.google.samples.apps.nowinandroid.demo.model.MovieSummary
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import javax.inject.Inject

/**
 * 电影列表视图模型
 * 负责管理电影数据的获取和状态管理
 */
@HiltViewModel
class MovieInfoViewModel @Inject constructor() : ViewModel() {
    /** 电影详情数据流 */
    private val _movieInfo = MutableStateFlow<MovieInfo?>(null)
    val movieInfo: StateFlow<MovieInfo?> = _movieInfo.asStateFlow()

    /** 加载状态数据流 */
    private val _isLoading = MutableStateFlow(false)
    val isLoading: StateFlow<Boolean> = _isLoading.asStateFlow()

    /** 错误信息数据流 */
    private val _error = MutableStateFlow<String?>(null)
    val error: StateFlow<String?> = _error.asStateFlow()

    /** Retrofit 实例，用于网络请求 */
    private val retrofit = Retrofit.Builder()
        .baseUrl(MovieApi.BASE_URL)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

    /** API 接口实例 */
    private val movieApi = retrofit.create(MovieApi::class.java)

    init {
        Log.d("tongkeqiang", "MovieListViewModel init block called")
//        loadMovieInfo( )
    }

    /**
     * 加载电影数据
     * 从 OMDB API 获取电影信息并更新状态
     */
    fun loadMovieInfo(title: String) {

        viewModelScope.launch {
            try {
                _isLoading.value = true
                _error.value = null
                Log.d("tongkeqiang", "Starting to load Movies")
                Log.d("tongkeqiang", "Requesting Movies from API")
                val movie = movieApi.getMovieInfo("2a251d3", "", title)
                _movieInfo.value = movie
                Log.d("tongkeqiang", "Processed Movies: " + movieInfo.value)
            } catch (e: Exception) {
                _error.value = e.message ?: "加载用户列表失败"
            } finally {
                _isLoading.value = false
                Log.d("tongkeqiang", "Finished loading Movies")
            }
        }
    }

    /**
     * 重试加载数据
     * 当加载失败时，可以调用此方法重新尝试加载
     */
    fun retry() {
//        loadMovieInfo()
    }
} 